package cn.iocoder.yudao.module.famous.doctor.controller.app.researchgroupuser;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.attention.vo.AttentionPageRespVO;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.researchgroupuser.vo.*;
import cn.iocoder.yudao.module.famous.doctor.convert.researchgroupuser.ResearchgroupUserConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.researchgroupuser.ResearchgroupUserDO;
import cn.iocoder.yudao.module.famous.doctor.service.researchgroupuser.ResearchgroupUserService;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserInfoRespVO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 课题组-用户关联")
@RestController
@RequestMapping("/famous.doctor/researchgroup-user/app")
@Validated
public class AppResearchgroupUserController {

    @Resource
    private ResearchgroupUserService researchgroupUserService;
    @Resource
    private MemberUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建课题组-用户关联")
    public CommonResult<Long> createResearchgroupUser(@Valid @RequestBody ResearchgroupUserCreateReqVO createReqVO) {
        return success(researchgroupUserService.createResearchgroupUser(createReqVO));
    }

    @PostMapping("/createList")
    @Operation(summary = "批量创建课题组-用户关联")
    public CommonResult<Long> createResearchgroupUserList(@Valid @RequestBody List<ResearchgroupUserCreateReqVO> createReqVO) {
        return success(researchgroupUserService.createResearchgroupUserList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课题组-用户关联")
    public CommonResult<Boolean> updateResearchgroupUser(@Valid @RequestBody ResearchgroupUserUpdateReqVO updateReqVO) {
        researchgroupUserService.updateResearchgroupUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课题组-用户关联")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteResearchgroupUser(@RequestParam("id") Long id) {
        researchgroupUserService.deleteResearchgroupUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课题组-用户关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ResearchgroupUserRespVO> getResearchgroupUser(@RequestParam("id") Long id) {
        ResearchgroupUserDO researchgroupUser = researchgroupUserService.getResearchgroupUser(id);
        return success(ResearchgroupUserConvert.INSTANCE.convert(researchgroupUser));
    }
    

    @GetMapping("/page")
    @Operation(summary = "获得课题组-用户关联分页")
    public CommonResult<PageResult<ResearchgroupUser>> getResearchgroupUserPage(@Valid ResearchgroupUserPageReqVO pageVO) {
        PageResult<ResearchgroupUserDO> pageResult = researchgroupUserService.getResearchgroupUserPage(pageVO);
        PageResult<ResearchgroupUser> resultList = ResearchgroupUserConvert.INSTANCE.convertResearchgroupUser(pageResult);
        for (int i = 0; i < resultList.getList().size(); i++) {
            if (resultList.getList().get(i).getUserType()!=null && resultList.getList().get(i).getUserType()==3) {
                //医生
                MemberUserDO user = userService.getUser(getLoginUserId());
                resultList.getList().get(i).setAppMemberUser(MemberUserConvert.INSTANCE.convert(user, null));
            } else if(resultList.getList().get(i).getUserType()!=null && resultList.getList().get(i).getUserType()==4){
                //患者
                resultList.getList().get(i).setAppMemberUser(JsonUtils.parseObject(resultList.getList().get(i).getUserSign(), AppMemberUserInfoRespVO.class));
            }
        }
        return success(resultList);
    }

    @GetMapping("/getgroupDockerPage")
    @Operation(summary = "获取该诊断人课题组下的医生")
    public CommonResult<IPage<ResearchgroupUserByClinic>> getgroupDockerPage(@Valid ResearchgroupUserPageReqVO pageVO) {
        IPage<ResearchgroupUserByClinic> pageResult = researchgroupUserService.getgroupDockerPage(pageVO);
        return success(pageResult);
    }


}
